<?php
error_reporting(E_ALL);
date_default_timezone_set('Asia/Shanghai');
define('API_URL', 'https://slog.slightech.com/realtime_data/add');
require_once 'Base_fetch.php';

	function get_cpu() {
		$value = Base_fetch::http_get($url);
		$token = Base_fetch::generate_token($key, $value, date('Y-m-d'));
		Base_fetch::http_post(API_URL, array('key' => $key, 'value' => $value, 'token' => $token));
	}

    function get_used_status() {
       $fp = popen('top -b -n 2 | grep -E "^(Cpu|Mem|Tasks)"',"r");
       $rs = "";
       while(!feof($fp)) {
            $rs.=fread($fp,1024);
       }
       pclose($fp);
       $sys_info = explode("\n",$rs);
       $tast_info = explode(",",$sys_info[3]);
       $cpu_info = explode(",",$sys_info[4]);
       $mem_info = explode(",",$sys_info[5]);
       $task_running = trim(trim($tast_info[1],'running'));   
       $cpu_free = trim(trim($cpu_info[3],'Cpu(s): '),'%id');  //cpu空闲量  
       $mem_total = trim(trim($mem_info[0],'Mem: '),'k total');
       $mem_used = trim($mem_info[1],'k used');
       $mem_usage = round(100*intval($mem_used)/intval($mem_total),2);  //内存使用百分比
       $fp = popen('df -lh | grep -E "^(/)"',"r");
       $rs = fread($fp,1024);
       pclose($fp);
       $rs = preg_replace("/\s{2,}/",' ',$rs);
       $hd = explode(" ",$rs);
       $hd_avail = trim($hd[3],'G'); //磁盘可用空间
       $hd_usage = trim($hd[4],'%'); //挂载点百分比
       $status = array(
               "mem_usage" => $mem_usage,
               "hd_avail"  => $hd_avail,
               "task_running" => $task_running,
               "hd_usage"  => $hd_usage,
               "cpu_free" => $cpu_free
              );
       
       
       return $status; 
    }     
    get_used_status();    
